package sunsun.xiaoli.jiarebang.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import sunsun.xiaoli.jiarebang.R;


/**
 * 版本更新
 */
public class UIAlertView02 extends Dialog {
    private Context context;
    private String title;
    private String message;
    private String buttonLeftText;
    private String buttonRightText;
    private UIAlertView02.ClickListenerInterface clickListenerInterface;

    private boolean isDownload=false;
    public UIAlertView02(Context context, String title, String message,
                         String buttonLeftText, String buttonRightText,boolean isDownload) {
        super(context, R.style.UIAlertViewStyle);

        this.context = context;
        this.title = title;
        this.message = message;
        this.buttonLeftText = buttonLeftText;
        this.buttonRightText = buttonRightText;
        this.isDownload=isDownload;
    }

    public interface ClickListenerInterface {
        public void doLeft();

        public void doRight();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        inite();
    }

    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.ui_alert_view03, null);
        setContentView(view);

        TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
        ImageView tvLeft = (ImageView) view.findViewById(R.id.tvBtnLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);

        tvTitle.setText("发现新版本,"+title+"来啦");
       // tvMessage.setText(Html.fromHtml(message));
        tvMessage.setText("发现新版本:"+title);
        tvLeft.setOnClickListener(new UIAlertView02.clickListener());
        tvRight.setOnClickListener(new UIAlertView02.clickListener());

        this.setCanceledOnTouchOutside(false);
        if (isDownload){
            tvLeft.setVisibility(View.GONE);
            this.setOnKeyListener(keylistener);
        }
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);

    }

    public void setClicklistener(UIAlertView02.ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class clickListener implements View.OnClickListener {


        public void onClick(View v) {

            int id = v.getId();
            switch (id) {
                case R.id.tvBtnLeft:
                    clickListenerInterface.doLeft();
                    break;
                case R.id.tvBtnRight:
                    clickListenerInterface.doRight();
                    break;

                default:
                    break;
            }
        }
    }

    OnKeyListener keylistener = new DialogInterface.OnKeyListener(){
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode== KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    } ;


}
